package com.zxh.zhenxihuan.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zxh.zhenxihuan.entity.z.Address;
import com.zxh.zhenxihuan.mapper.AddressMapper;
import com.zxh.zhenxihuan.service.AddressService;
import com.zxh.zhenxihuan.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:geQiang
 * @version:1.0
 * @date:2019/8/8
 * @description:com.zxh.zhenxihuan.controller
 */
@RestController
@CrossOrigin
public class AddressController {
    @Resource
    private AddressService addressService;
    @Resource
    AddressMapper addressMapper;


    @PostMapping("/findAddress")
    public String findAll(@RequestBody Map maps){
        try {
            Integer num= (Integer) maps.get("page");
            Integer count= (Integer) maps.get("limit");
            String uid=(String) maps.get("uid");
            Page<Address> pages=addressService.page(num,count,uid);
            Map<String,Object> map=new HashMap<>();
            map.put("page",pages);
            System.out.println(pages);
            System.out.println("findAll");
            return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd", SerializerFeature.DisableCircularReferenceDetect);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加地址
     */
    @PostMapping("/addressAdd")
    public Long AddressAdd(@RequestBody Address address){
//        String uid=(String) maps.get("uid");
//        String addr=(String) maps.get("address");
//        String tel=(String) maps.get("tel");
        System.out.println(address+">>>>>>>>>>>>>>");

        return addressMapper.add(address);
    }

    /**
     * 商品删除
     * @return
     */
    @PostMapping("/addressDel")
    public String AddressDel(@RequestBody Map maps){
        Integer id=(Integer) maps.get("id");
        Integer num=addressService.del(id);
        Map<String,Object> map=new HashMap<>();
        if(num>0){
            System.out.println("刪除成功");
        }
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss",SerializerFeature.DisableCircularReferenceDetect);
    }

}
